2 * Big integer class, optimized for decimal integers.
3 * Stores and manipulates integers represented as byte arrays,
4 * where each byte is a decimal digit. If you're looking for
5 * robust, bug-free, efficient code, keep looking. This is a quick
6 * and dirty hack. Some day I'll write a templatized BigInt, where
7 * you will be able to select the base in which to store the
8 * number. When that day comes, most of this code will be thrown
12 * operator-(int) does not work.
14 * BigInt doesn't play nice with long long. Either use int
18 * - capacity is never smaller than 16
19 * - capacity is not the smallest it can be because every
20 * modifying member function first grows digits as much as
21 * it might ever need and then does its job.
23 * - Passed numerous problems on Valladolid, including
24 * 107, 288, 324, 424, 465, 485, 495, 560, 619, 623, etc.
27 * - This class was written for the g++ compiler and uses some
28 * of the g++ extensions (like "long double" and the ">?="
29 * operator). If you want to compile this under Micro$oft's
30 * "compiler", I don't want to talk to you.
32 * LAST MODIFIED: October 5, 2005
34 * This file is part of my library of algorithms found here:
35 * http://www.palmcommander.com:8081/tools/
37 * http://www.palmcommander.com:8081/tools/LICENSE.html
38 * Copyright (c) 2002-2004
53 #define min(x,y) ((x) < (y) ? (x) : (y))
57 #define max(x,y) ((x) > (y) ? (x) : (y))
64 int size
; // number of used bytes (digits)
65 int capacity
; // size of digits
66 int sign
; // -1, 0 or +1
69 /** Creates a BigInt with initial value n and initial capacity cap **/
70 BigInt( int n
, int cap
);
72 /** Creates a BigInt with initial value n **/
75 /** Creates a BigInt with initial value floor( d ) **/
76 BigInt( long double d
);
78 /** Creates a BigInt with value 0 **/
81 /** Creates a BigInt by reading the value from a string **/
84 /** Creates a BigInt by reading the value from a C string **/
85 BigInt( const char s
[] );
87 /** Copy constructor **/
88 BigInt( const BigInt
&n
);
90 /** Assignment operators **/
91 const BigInt
&operator=( const BigInt
&n
);
92 const BigInt
&operator=( int n
);
97 /** Removes any leading zeros and adjusts the sign **/
100 /** Returns the sign of n: -1, 0 or 1 **/
101 static int sig( int n
);
103 /** Returns the sign of n: -1, 0 or 1 **/
104 static int sig( long double n
);
106 /** Returns the number of decimal digits **/
107 inline int length() { return size
; }
111 BigInt
operator++( int );
113 BigInt
operator--( int );
115 BigInt
operator+ ( int n
);
116 BigInt
operator+ ( BigInt n
);
117 BigInt
&operator+=( int n
);
118 BigInt
&operator+=( BigInt n
);
119 BigInt
operator- ( int n
);
120 BigInt
operator- ( BigInt n
);
121 BigInt
&operator-=( int n
);
122 BigInt
&operator-=( BigInt n
);
123 BigInt
operator* ( int n
);
124 BigInt
operator* ( BigInt n
);
125 void operator*=( int n
);
126 void operator*=( BigInt n
);
127 BigInt
operator/ ( int n
);
128 BigInt
operator/ ( BigInt n
);
129 void operator/=( int n
);
130 void operator/=( BigInt n
);
131 int operator% ( int n
);
132 BigInt
operator% ( BigInt n
);
133 void operator%=( int n
);
134 void operator%=( BigInt n
);
135 int divide( int n
); // Divides storing quotient in *this and returning the remainder
136 BigInt
divide( BigInt n
); // Divides storing quotient in *this and returning the remainder
137 BigInt
operator* ( long double n
); // Multiplies by a double and truncates (always under-estimates!)
138 void operator*=( long double n
); // Multiplies by a double and truncates (always under-estimates!)
140 /** Bitwise arithmetic **/
141 BigInt
operator<< ( int n
);
142 void operator<<=( int n
);
143 BigInt
operator>> ( int n
); // Works differently for negative numbers
144 void operator>>=( int n
); // Works differently for negative numbers
146 BigInt operator& ( int n );
147 BigInt operator& ( BigInt n );
148 void operator&= ( int n );
149 void operator&= ( BigInt n );
150 BigInt operator| ( int n );
151 BigInt operator| ( BigInt n );
152 void operator|= ( int n );
153 void operator|= ( BigInt n );
154 BigInt operator^ ( int n );
155 BigInt operator^ ( BigInt n );
156 void operator^= ( int n );
157 void operator^= ( BigInt n );
160 /** Concatenation ;-) **/
161 BigInt
operator,( int n
);
162 BigInt
operator,( BigInt n
);
167 //operator int(); //XXX: Don't do this!!! It takes precedence over operator+(int,BigInt)
171 bool operator<( BigInt n
);
172 bool operator>( BigInt n
);
173 bool operator==( BigInt n
);
174 bool operator<=( BigInt n
);
175 bool operator>=( BigInt n
);
176 bool operator<( int n
);
177 bool operator>( int n
);
178 bool operator==( int n
);
179 bool operator<=( int n
);
180 bool operator>=( int n
);
181 int compare( BigInt n
);
183 /** Returns the lowest value as an integer (watch out for overflow) **/
186 /** Returns the value as a decimal string **/
189 /** Outputs decimal value to stdout **/
192 /** Outputs the decimal value, with commas **/
193 void printWithCommas( ostream
&out
);
200 friend istream
&operator>>( istream
&in
, BigInt
&n
);
201 friend ostream
&operator<<( ostream
&out
, BigInt n
);
204 friend long double log2( BigInt x
, long double epsilon
);
205 inline friend long double log( BigInt x
, long double epsilon
);
206 inline friend long double log10( BigInt x
, long double epsilon
);
207 inline friend long double lg( BigInt x
, long double epsilon
);
208 inline friend long double ln( BigInt x
, long double epsilon
);
211 BigInt
operator+( int m
, BigInt
&n
)
216 BigInt
operator-( int m
, BigInt
&n
)
221 BigInt
operator*( int m
, BigInt
&n
)
226 BigInt
operator/( int m
, BigInt
&n
)
228 return BigInt( m
) / n
;
231 BigInt
operator%( int m
, BigInt
&n
)
233 return BigInt( m
) % n
;
237 inline bool isDigit( int c
)
239 return( c
>= ( int )'0' && c
<= ( int )'9' );
243 istream
&operator>>( istream
&in
, BigInt
&n
) // FIXME: see inside
249 while( ( c
= in
.peek() ) >= 0 &&
250 ( c
== ' ' || c
== '\t' || c
== '\r' || c
== '\n' ) )
252 if( c
< 0 || ( c
!= ( int )'-' && !isDigit( c
) ) )
254 in
>> c
; // XXX: force in.fail()
257 if( c
== ( int )'-' ) { sign
= -1; in
.get(); }
259 // FIXME: Extremely inefficient! Use a string.
260 while( ( c
= in
.peek() ) >= 0 && isDigit( c
) )
264 n
+= ( c
- ( int )'0' );
266 n
.sign
= sign
; //XXX: assign n.sign directly after fixing the FIXME
271 ostream
&operator<<( ostream
&out
, BigInt n
) //FIXME: make more efficient
273 return out
<< n
.toString();
276 BigInt::BigInt( int n
, int cap
)
278 cap
= max( cap
, ( int )sizeof( n
) * 8 );
282 digits
= new char[cap
];
283 memset( digits
, 0, cap
);
284 for( size
= 0; n
; size
++ )
286 digits
[size
] = n
% 10;
291 BigInt::BigInt( int n
)
296 digits
= new char[capacity
];
297 memset( digits
, 0, capacity
);
301 digits
[size
++] = n
% 10;
306 BigInt::BigInt( long double d
)
309 sign
= ( d
< 0 ? -1 : d
> 0 ? 1 : 0 );
311 digits
= new char[capacity
];
312 memset( digits
, 0, capacity
);
317 digits
[size
++] = 0 >? ( int )( ( d
- floor( d
/ 10 ) * 10 ) + 0.5 ) <? 9;
326 digits
= new char[capacity
];
327 memset( digits
, 0, capacity
);
331 BigInt::BigInt( string s
)
333 capacity
= max( ( int )s
.size(), 16 );
335 digits
= new char[capacity
];
336 memset( digits
, 0, capacity
);
338 istringstream
in( s
);
342 BigInt::BigInt( const char s
[] )
344 capacity
= max( ( int )strlen( s
), 16 );
346 digits
= new char[capacity
];
347 memset( digits
, 0, capacity
);
349 istringstream
in( s
);
353 BigInt::BigInt( const BigInt
&n
)
355 capacity
= n
.capacity
;
358 digits
= new char[capacity
];
359 memcpy( digits
, n
.digits
, capacity
);
362 const BigInt
&BigInt::operator=( const BigInt
&n
)
366 if( capacity
< n
.size
)
368 capacity
= n
.capacity
;
370 digits
= new char[capacity
];
374 memcpy( digits
, n
.digits
, size
);
375 memset( digits
+ size
, 0, capacity
- size
);
380 const BigInt
&BigInt::operator=( int n
)
384 for( size
= 0; n
; size
++ )
386 digits
[size
] = n
% 10;
397 void BigInt::normalize()
399 while( size
&& !digits
[size
-1] ) size
--;
400 if( !size
) sign
= 0;
403 int BigInt::sig( int n
)
405 return( n
> 0 ? 1 : ( n
== 0 ? 0 : -1 ) );
408 int BigInt::sig( long double n
)
410 return( n
> 0 ? 1 : ( n
== 0 ? 0 : -1 ) );
416 for( int i
= size
- 1; i
>= 0; i
-- )
420 if( result
< 0 ) return sign
* 0x7FFFFFFF;
422 return sign
* result
;
425 string
BigInt::toString()
427 string s
= ( sign
>= 0 ? "" : "-" );
428 for( int i
= size
- 1; i
>= 0; i
-- )
429 s
+= ( digits
[i
] + '0' );
430 if( size
== 0 ) s
+= '0';
434 void BigInt::print() //FIXME: make more efficient
439 void BigInt::printWithCommas( ostream
&out
)
441 if( sign
< 0 ) out
.put( '-' );
442 for( int i
= size
- 1; i
>= 0; i
-- )
444 out
.put( digits
[i
] + '0' );
445 if( !( i
% 3 ) && i
) out
.put( ',' );
447 if( size
== 0 ) out
.put( '0' );
452 char *olddigits
= digits
;
453 int oldCap
= capacity
;
455 digits
= new char[capacity
];
456 memcpy( digits
, olddigits
, oldCap
);
457 memset( digits
+ oldCap
, 0, oldCap
);
461 BigInt
BigInt::operator++()
467 BigInt
BigInt::operator++( int )
472 BigInt
BigInt::operator--()
478 BigInt
BigInt::operator--( int )
483 BigInt
BigInt::operator-()
485 BigInt
result( *this );
490 BigInt
BigInt::operator+( int n
)
492 BigInt
result( *this );
497 BigInt
BigInt::operator+( BigInt n
)
499 BigInt
result( *this );
504 BigInt
&BigInt::operator+=( int n
)
506 if( size
== capacity
) grow();
508 int nsign
= sig( n
);
509 if( !nsign
) return *this;
510 if( !sign
) sign
= nsign
;
516 for( i
= 0; n
|| carry
; i
++ )
519 int newdig
= digits
[i
] + dig
+ carry
;
520 digits
[i
] = newdig
% 10;
524 size
= max( i
, size
);
526 else operator-=( -n
);
530 BigInt
&BigInt::operator+=( BigInt n
)
532 int maxS
= max( size
, n
.size
) + 1;
533 while( maxS
>= capacity
) grow(); //FIXME: this is stupid
535 if( !n
.sign
) return *this;
536 if( !sign
) sign
= n
.sign
;
541 for( i
= 0; i
< maxS
- 1 || carry
; i
++ )
544 if( i
< size
) newdig
+= digits
[i
];
545 if( i
< n
.size
) newdig
+= n
.digits
[i
];
546 digits
[i
] = newdig
% 10;
549 size
= max( i
, size
);
560 BigInt
BigInt::operator-( int n
)
562 BigInt
result( *this );
567 BigInt
BigInt::operator-( BigInt n
)
569 BigInt
result( *this );
574 BigInt
&BigInt::operator-=( int n
)
576 if( size
== capacity
) grow();
578 int nsign
= sig( n
);
579 if( !nsign
) return *this;
580 if( !sign
) sign
= 1;
584 if( sign
>= 0 && *this < bin
|| sign
< 0 && *this > bin
)
586 // Subtracting a bigger number
587 operator=( toInt() - n
);
594 for( i
= 0; n
|| carry
; i
++ )
597 int newdig
= digits
[i
] - dig
+ carry
;
598 if( newdig
< 0 ) newdig
+= 10, carry
= -1;
605 else operator+=( -n
);
609 BigInt
&BigInt::operator-=( BigInt n
)
611 int maxS
= max( size
, n
.size
) + 1;
612 while( maxS
>= capacity
) grow(); //FIXME: this is stupid
614 if( !n
.sign
) return *this;
615 if( !sign
) sign
= 1;
618 if( sign
>= 0 && *this < n
|| sign
< 0 && *this > n
)
620 // Subtracting a bigger number
630 for( i
= 0; i
< maxS
- 1; i
++ )
633 if( i
< size
) newdig
+= digits
[i
];
634 if( i
< n
.size
) newdig
-= n
.digits
[i
];
635 if( newdig
< 0 ) newdig
+= 10, carry
= -1;
639 if( carry
) // Subtracted a bigger number, need to flip sign
641 if( i
) digits
[0] = 10 - digits
[0];
642 size
= ( i
? 1 : 0 );
643 for( int j
= 1; j
< i
; j
++ )
645 digits
[j
] = 9 - digits
[j
];
646 if( digits
[i
] ) size
= j
+ 1;
661 BigInt
BigInt::operator*( int n
)
663 BigInt
result( 0, size
+ ( int )sizeof( n
) * 8 );
664 int nsign
= sig( n
);
666 result
.sign
= sign
* nsign
;
667 if( !result
.sign
) return result
;
676 for( j
= 0; j
< size
|| carry
; j
++ )
678 int newDig
= result
.digits
[i
+ j
] + ( j
< size
? dig
* digits
[j
] : 0 ) + carry
;
679 result
.digits
[i
+ j
] = newDig
% 10;
685 result
.size
= i
+ j
- 1;
689 BigInt
BigInt::operator*( BigInt n
)
691 BigInt
result( 0, size
+ n
.size
);
693 result
.sign
= sign
* n
.sign
;
694 if( !result
.sign
) return result
;
697 for( i
= 0; i
< n
.size
; i
++ )
702 for( j
= 0; j
< size
|| carry
; j
++ )
705 result
.digits
[i
+ j
] +
706 ( j
< size
? n
.digits
[i
] * digits
[j
] : 0 ) +
708 result
.digits
[i
+ j
] = newDig
% 10;
713 result
.size
= i
+ j
- 1;
718 void BigInt::operator*=( int n
)
720 operator=( operator*( n
) );
723 void BigInt::operator*=( BigInt n
)
725 operator=( operator*( n
) );
728 BigInt
BigInt::operator/( int n
)
730 if( !n
) n
/= n
; //XXX: force a crash
732 BigInt
result( *this );
737 BigInt
BigInt::operator/( BigInt n
)
739 if( !n
) n
.size
/= n
.size
; //XXX: force a crash
741 BigInt
result( *this );
746 void BigInt::operator/=( int n
)
751 void BigInt::operator/=( BigInt n
)
756 int BigInt::operator%( int n
)
759 return tmp
.divide( n
);
762 void BigInt::operator%=( int n
)
764 operator=( divide( n
) );
767 BigInt
BigInt::operator%( BigInt n
)
770 return tmp
.divide( n
);
773 void BigInt::operator%=( BigInt n
)
775 operator=( divide( n
) );
778 int BigInt::divide( int n
)
780 if( !n
) n
/= n
; //XXX: force a crash
782 int nsign
= sig( n
);
784 if( !sign
) return 0;
788 for( int i
= size
- 1; i
>= 0; i
-- )
793 tmp
-= digits
[i
] * n
;
799 BigInt
BigInt::divide( BigInt n
)
801 if( !n
) n
.size
/= n
.size
; //XXX: force a crash
803 if( !sign
) return 0;
806 int oldSign
= n
.sign
;
809 BigInt
tmp( 0, size
);
810 for( int i
= size
- 1; i
>= 0; i
-- )
815 while( tmp
>= n
) { tmp
-= n
; digits
[i
]++; }
824 // This is only exact to the first 15 or so digits, but it is
825 // never an over-estimate
826 BigInt
BigInt::operator*( long double n
)
828 // the number of digits after the decimal point to use
829 int DIGS_AFTER_DOT
= 15;
831 int nsign
= sig( n
);
833 int ndigs
= n
>= 1 ? ( int )log10( n
) + 1 : 0;
834 BigInt
result( 0, size
+ ndigs
);
835 result
.sign
= sign
* nsign
;
836 if( !result
.sign
) return result
;
838 if( n
>= 1 ) for( int i
= 0; i
< ndigs
; i
++ ) n
/= 10;
841 char afterDot
[DIGS_AFTER_DOT
+ 1];
842 memset( afterDot
, 0, sizeof( afterDot
) );
844 // Keep going until the DIGS_AFTER_DOT'th digit after the decimal point
845 for( int i
= ndigs
- 1; i
>= -DIGS_AFTER_DOT
; i
-- )
848 int dig
= ( int )floor( n
);
853 for( int j
= 0; j
< size
|| carry
; j
++ )
856 ( i
+ j
< 0 ? afterDot
[-( i
+ j
)] : result
.digits
[i
+ j
] )
859 ( i
+ j
< 0 ? afterDot
[-( i
+ j
)] : result
.digits
[i
+ j
] ) = newdig
% 10;
860 if( i
+ j
>= 0 && result
.digits
[i
+ j
] ) result
.size
>?= i
+ j
+ 1;
864 if( !result
.size
) result
.sign
= 0;
868 void BigInt::operator*=( long double n
)
870 operator=( operator*( n
) );
873 BigInt
BigInt::operator<<( int n
)
875 BigInt
result( *this );
880 void BigInt::operator<<=( int n
)
882 if( n
< 0 ) operator>>=( -n
);
885 BigInt
mult( 1, 4 * n
);
886 for( int i
= ( 1 << 30 ); i
; i
>>= 1 )
889 if( n
& i
) mult
*= 2;
895 BigInt
BigInt::operator>>( int n
)
897 BigInt
result( *this );
902 void BigInt::operator>>=( int n
)
904 if( n
< 0 ) operator<<=( -n
);
907 BigInt
mult( 1, 4 * n
);
908 for( int i
= ( 1 << 30 ); i
; i
>>= 1 )
911 if( n
& i
) mult
*= 2;
917 BigInt BigInt::operator&( int n )
921 BigInt BigInt::operator&( BigInt n )
925 void BigInt::operator&=( int n )
929 void BigInt::operator&=( BigInt n )
933 BigInt BigInt::operator|( int n )
937 BigInt BigInt::operator|( BigInt n )
941 void BigInt::operator|=( int n )
945 void BigInt::operator|=( BigInt n )
949 BigInt BigInt::operator^( int n )
953 BigInt BigInt::operator^( BigInt n )
957 void BigInt::operator^=( int n )
961 void BigInt::operator^=( BigInt n )
965 BigInt BigInt::operator~()
969 BigInt
BigInt::operator,( int n
)
971 BigInt
result( 0, size
+ ( int )sizeof( n
) * 8 );
972 for( result
.size
= 0; n
; result
.size
++ )
974 result
.digits
[result
.size
] = n
% 10;
977 memcpy( result
.digits
+ result
.size
, digits
, size
* sizeof( digits
[0] ) );
984 BigInt
BigInt::operator,( BigInt n
)
986 BigInt
result( 0, size
+ n
.size
);
987 memcpy( result
.digits
, n
.digits
, n
.size
* sizeof( n
.digits
[0] ) );
988 memcpy( result
.digits
+ n
.size
, digits
, size
* sizeof( digits
[0] ) );
989 result
.size
= size
+ n
.size
;
995 bool BigInt::operator!()
1000 BigInt::operator bool()
1005 //BigInt::operator int()
1010 BigInt::operator string()
1015 bool BigInt::operator<( BigInt n
)
1017 return( compare( n
) < 0 );
1020 bool BigInt::operator>( BigInt n
)
1022 return( compare( n
) > 0 );
1025 bool BigInt::operator==( BigInt n
)
1027 return( compare( n
) == 0 );
1030 bool BigInt::operator<=( BigInt n
)
1032 return( compare( n
) <= 0 );
1035 bool BigInt::operator>=( BigInt n
)
1037 return( compare( n
) >= 0 );
1040 bool BigInt::operator<( int n
)
1042 return( compare( BigInt( n
) ) < 0 );
1045 bool BigInt::operator>( int n
)
1047 return( compare( BigInt( n
) ) > 0 );
1050 bool BigInt::operator==( int n
)
1052 return( compare( BigInt( n
) ) == 0 );
1055 bool BigInt::operator<=( int n
)
1057 return( compare( BigInt( n
) ) <= 0 );
1060 bool BigInt::operator>=( int n
)
1062 return( compare( BigInt( n
) ) >= 0 );
1065 int BigInt::compare( BigInt n
)
1067 if( sign
< n
.sign
) return -1;
1068 if( sign
> n
.sign
) return 1;
1069 if( size
< n
.size
) return -sign
;
1070 if( size
> n
.size
) return sign
;
1071 for( int i
= size
- 1; i
>= 0; i
-- )
1073 if( digits
[i
] < n
.digits
[i
] ) return -sign
;
1074 else if( digits
[i
] > n
.digits
[i
] ) return sign
;
1079 long double log2( BigInt x
, long double epsilon
= 0.000000000000001 )
1081 static /* const */ long double O
= 0.0;
1082 if( x
.sign
<= 0 ) return O
/ O
; // Return NaN
1084 long double y
= 0.0, z
= 1.0, f
= 0.0;
1087 if( x
.divide( 2 ) ) f
+= 1.0;
1092 while( z
> epsilon
)
1105 inline long double log( BigInt x
, long double epsilon
= 0.000000000000001 )
1107 return log2( x
, epsilon
) * 0.6931471805599;
1110 inline long double log10( BigInt x
, long double epsilon
= 0.000000000000001 )
1112 return log2( x
, epsilon
) * 0.301029995664;
1115 inline long double lg( BigInt x
, long double epsilon
= 0.000000000000001 )
1117 return log2( x
, epsilon
);
1120 inline long double ln( BigInt x
, long double epsilon
= 0.000000000000001 )
1122 return log( x
, epsilon
);
1134 cout
<< (x
- y
) << endl
;
1137 cout << "Constructors and copy constructors:" << endl;
1138 cout << "12345000 = " << BigInt( 12345000 ) << endl;
1139 BigInt b = BigInt( 12345000 );
1140 cout << "12345000 = " << b << endl;
1142 cout << "12345000 = " << c << endl;
1143 cout << "1234567890 = " << BigInt( ( long double )1234567890.49999 ) << endl;
1146 cout << "Addition and subtraction:" << endl;
1147 cout << "123 + 234 = " << ( BigInt( 123 ) + 234 ).toInt() << endl;
1148 cout << "243 + 999 = " << ( BigInt( 243 ) + BigInt( 999 ) ) << endl;
1149 cout << "-123 + -321 = " << ( BigInt( -123 ) + BigInt( -321 ) ) << endl;
1150 cout << "-123 + 321 = " << ( BigInt( -123 ) + BigInt( 321 ) ) << endl;
1151 cout << "-2 + 5 = " << ( BigInt( -2 ) + 5 ) << endl;
1152 cout << "-2 + 5 = " << ( BigInt( -2 ) + BigInt( 5 ) ) << endl;
1153 cout << "-2 + -5 = " << ( BigInt( -2 ) + -5 ) << endl;
1154 cout << "-2 + -5 = " << ( BigInt( -2 ) + BigInt( -5 ) ) << endl;
1155 cout << "0 + -5 = " << ( BigInt( 0 ) + -5 ) << endl;
1156 cout << "0 + -5 = " << ( BigInt( 0 ) + BigInt( -5 ) ) << endl;
1157 cout << "4567 - 1234 = " << ( 4567 - BigInt( 1234 ) ) << endl;
1158 cout << "345 - 46 = " << ( BigInt( 345 ) - BigInt( 46 ) ) << endl;
1159 cout << "2 - 6 = " << ( BigInt( 2 ) - BigInt( 6 ) ) << endl;
1160 cout << "2 - 6 = " << ( BigInt( 2 ) - 6 ) << endl;
1161 cout << "0 - 5 = " << ( BigInt( 0 ) - 5 ) << endl;
1162 cout << "0 - 5 = " << ( BigInt( 0 ) - BigInt( 5 ) ) << endl;
1163 cout << "0 - -5 = " << ( BigInt( 0 ) - -5 ) << endl;
1164 cout << "0 - -5 = " << ( BigInt( 0 ) - BigInt( -5 ) ) << endl;
1165 cout << "10000 - 10000 = " << ( BigInt( 10000 ) - 10000 ) << endl;
1166 cout << "10000 - 10110 = " << ( BigInt( 10000 ) - 10110 ) << endl;
1167 cout << "4567 - 4568 = " << ( BigInt( 4567 ) - 4568 ) << endl;
1168 cout << "-4567 - -4568 = " << ( BigInt( -4567 ) - BigInt( -4568 ) ) << endl;
1169 cout << "999 - 9999 = " << ( BigInt( 999 ) - 9999 ) << endl;
1170 cout << "2000000000 + 2000000000 + 2000123456 = " << ( BigInt( 2000000000 ) + 2000000000 + BigInt( 2000123456 ) ) << endl;
1171 cout << "-34567 + 34568 = " << ( BigInt( -34567 ) + BigInt( 34568 ) ) << endl;
1172 cout << "10 - 1 = " << ( BigInt( 10 ) - 1 ) << endl;
1173 cout << "1 - 10 = " << ( BigInt( 1 ) - 10 ) << endl;
1174 cout << "Fib( 613 ) + Fib( 614 ) = " << (
1175 BigInt( "57535841731394367586444934959935162731893485882113791734636043664022186311322175066312007025864665068095897804714985049873571833" )
1177 BigInt( "93094947492730684688120544687306111880728698574224279139760379700550384193434187688727692133714165658764281830930007773906603177" )
1180 cout << "Multiplication/division:" << endl;
1181 cout << "128 * 512 = " << ( BigInt( 128 ) * 512 ) << endl;
1182 cout << "0 * 12345 = " << ( BigInt( 0 ) * 12345 ) << endl;
1183 cout << "-123 * 0 = " << ( BigInt( -123 ) * 0 ) << endl;
1184 cout << "-12345 * 1000001 = " << ( BigInt( -12345 ) * BigInt( 1000001 ) ) << endl;
1185 cout << "-1 * -1 = " << ( BigInt( -1 ) * BigInt( -1 ) ) << endl;
1186 cout << "1024 / 2 = " << ( BigInt( 1024 ) / 2 ) << endl;
1187 cout << "-525474 / -789 = " << ( BigInt( -525474 ) / -789 ) << endl;
1188 cout << "-81 / 27 = " << ( BigInt( -81 ) / 27 ) << endl;
1189 cout << "0 / -888 = " << ( BigInt( 0 ) / -888 ) << endl;
1190 cout << "1024 / 2 = " << ( BigInt( 1024 ) / BigInt( 2 ) ) << endl;
1191 cout << "-525474 / -789 = " << ( BigInt( -525474 ) / BigInt( -789 ) ) << endl;
1192 cout << "-81 / 27 = " << ( BigInt( -81 ) / BigInt( 27 ) ) << endl;
1193 cout << "0 / -888 = " << ( BigInt( 0 ) / BigInt( -888 ) ) << endl;
1195 int rem = q.divide( 2 );
1196 cout << "1023 / 2 = " << q << " + " << rem << "/2" << endl;
1198 rem = q.divide( 98765 );
1199 cout << "1219255159 / 98765 = " << q << " + " << rem << "/98765" << endl;
1201 rem = q.divide( 11 );
1202 cout << "121 / 11 = " << q << " + " << rem << "/11" << endl;
1204 BigInt rem2 = q.divide( BigInt( 2 ) );
1205 cout << "1023 / 2 = " << q << " + " << rem2 << "/2" << endl;
1207 rem2 = q.divide( BigInt( 98765 ) );
1208 cout << "1219255159 / 98765 = " << q << " + " << rem2 << "/98765" << endl;
1210 rem2 = q.divide( BigInt( 11 ) );
1211 cout << "121 / 11 = " << q << " + " << rem2 << "/11" << endl;
1213 rem2 = q.divide( BigInt( 9 ) );
1214 cout << "9999 / 9 = " << q << " + " << rem2 << "/9" << endl;
1215 cout << "1024 * 15.37 = " << BigInt( 1024 ) * 15.37l << endl;
1216 cout << "100 * 0.5 = " << BigInt( 100 ) * 0.5l << endl;
1217 cout << "123456789 * 0.123456789 = " << BigInt( 123456789 ) * 0.123456789l << endl;
1218 cout << "4286 * -0.5 = " << BigInt( 4286 ) * -0.5l << endl;
1219 cout << "29384723 * 1.0 = " << BigInt( 29384723 ) * 1.0l << endl;
1220 cout << "29384723 * -1.0 = " << BigInt( 29384723 ) * -1.0l << endl;
1221 cout << "3874928345 * 0.0 = " << BigInt( "3874928345" ) * 0.0l << endl;
1223 cout << "n = 1000: n*n*(8+n*(12+n*(3+n*n))) = " << n*n*(8+n*(12+n*(3+n*n))) << endl;
1226 cout << "Concatenation:" << endl;
1227 cout << "123,456 = " << ( BigInt( 123 ), 456 ) << endl;
1228 cout << "9999,55 = " << ( BigInt( 9999 ), BigInt( 55 ) ) << endl;
1229 cout << "0,1 = " << ( BigInt( 0 ), BigInt( 1 ) ) << endl;
1230 cout << "0,0 = " << ( BigInt( 0 ), 0 ) << endl;
1231 cout << "0,0 = " << ( BigInt( 0 ), BigInt( 0 ) ) << endl;
1233 cout << "Reflection:" << endl;
1235 cout << "11 + 11 = " << ( x + x ) << endl;
1236 cout << "11 * 11 = " << ( x * x ) << endl;
1238 cout << "11 + 11 = " << x << endl;
1240 cout << "22 * 22 = " << x << endl;
1242 cout << "Bitwise operations:" << endl;
1243 cout << "1 << 10 = " << ( 1 << 10 ) << " = " << ( BigInt( 1 ) << 10 ) << endl;
1244 cout << "-7 << 2 = " << ( -7 << 2 ) << " = " << ( BigInt( -7 ) << 2 ) << endl;
1245 cout << "3 << 8 = " << ( 3 << 8 ) << " = " << ( BigInt( 3 ) << 8 ) << endl;
1246 cout << "3 << 9 = " << ( 3 << 9 ) << " = " << ( BigInt( 3 ) << 9 ) << endl;
1247 cout << "1024 >> 9 = " << ( 1024 >> 9 ) << " = " << ( BigInt( 1024 ) >> 9 ) << endl;
1248 cout << "-1 >> 4 = " << (-1 >> 4) << " = " << ( BigInt( -1 ) >> 4 ) << endl;
1250 cout << "Input:" << endl;
1251 istringstream in( "1234567890" );
1253 cout << "1234567890 = " << x << endl;
1254 istringstream in2( " \t\n\r01234 -00009876\t" );
1256 cout << "1234 = " << x << endl;
1257 cout << "-9876 = " << rem2 << endl;
1259 cout << "Exhaustion:" << endl;
1260 BigInt *table = new BigInt[10240];
1261 table[0] = 0; table[1] = 1;
1262 for( int i = 2; i < 10240; i++ )
1263 table[i] = table[i - 1] + table[i - 2];
1264 cout << "Fibonacci( 615 ) = " << table[615] << endl;
1265 cout << "Fibonacci( 10000 ) = " << table[10000] << endl;
1268 cout << "Logarithms" << endl;
1269 cout << "log2( 1024 ) = " << log2( BigInt( 1024 ) ) << endl;
1270 cout << "log2( 6 ) = " << log2( BigInt( 6 ) ) << endl;
1271 cout << "log( 0 ) = " << log( BigInt( 0 ) ) << endl;
1272 cout << "log10( 1000000 ) = " << log10( BigInt( 10000000 ) ) << endl;
1273 cout << "log( 1234567 ) = " << log( BigInt( 1234567 ) ) << endl;